using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Dune.Business.Managers;
using Edge.Common.Services;
using Edge.Common.Services;

namespace Edge.Common.Web.DataSourceAdapters
{
    public interface IFilterHolder<TFilter>
         where TFilter : class, new()
    {
        TFilter Filter
        {
            get;
        }
    }

    /// <summary>
    /// Summary description for FilteredDataSourceView
    /// </summary>
    public class FilteredDataSourceView<TDalService, TEntity, TFilter> : BaseDataSourceView
        where TDalService : IFilteredSearch<TEntity, TFilter>
        where TFilter : class, new()
    {

        private IFilterHolder<TFilter> filterHolder = null;

        public FilteredDataSourceView(IDataSource owner, string viewName, IFilterHolder<TFilter> filterHolder)
            : base(owner, viewName)
        {
            if (filterHolder == null)
                throw new ArgumentException("filterHolder");
            this.filterHolder = filterHolder;
        }


        public IFilterHolder<TFilter> FilterHolder
        {
            get { return filterHolder; }
        }

        protected override IEnumerable Find(FetchOptions fetchOptions)
        {
            TDalService service = Locator.GetService<TDalService>();
            ICollection<TEntity> result = service.Find(fetchOptions, filterHolder.Filter);
            return result;
        }

        protected override int Count()
        {
            TDalService service = Locator.GetService<TDalService>();
            return service.Count(filterHolder.Filter);
        }
    }
}
